import Vue from 'vue';
import Router from 'vue-router';
import { ComponentShell } from 'ibiz-vue';
import { PreviewUtil } from '@/utils/preview-util/preview-util';
Vue.use(Router);

const router = new Router({
    routes: [
        {
            name: 'app',
            path: '/app/:type([app|view|control]{1})?/:modelPath?',
            beforeEnter: (to: any, from: any, next: any) => {
                PreviewUtil.getInstance().initPreview(router, to).then((result: any) => {
                    next(result ? true : false);
                }).catch((error) => {
                    console.error(error);
                    next(false);
                })
            },
            meta: {
                captionTag: '',
                caption: '组件壳',
                viewType: 'APPINDEX',
                info: '',
                parameters: [
                    { pathName: 'views', parameterName: 'app' },
                ],
                requireAuth: false,
            },
            component: ComponentShell
        },
        {
            path: '/lock',
            name: 'lock',
            meta: {
                captionTag: '',
                caption: '锁屏',
                viewType: 'lock',
                requireAuth: false,
                ignoreAddPage: true,
            },
            component: () => import('@components/app-lock/app-lock.vue'),
        },
        {
            path: '/appredirectview',
            name: 'appredirectview',
            meta: {
                captionTag: '',
                caption: '全局应用重定向视图',
                viewType: 'REDIRECTVIEW',
                requireAuth: false,
                ignoreAddPage: true,
            },
            component: () => import('@components/app-redirect-view/app-redirect-view.vue'),
        },
        {
            name: '404',
            path: '/404',
            component: () => import('@components/404/404.vue')
        },
        {
            path: '/login/:login?',
            name: 'login',
            meta: {
                captionTag: '',
                caption: '登录',
                viewType: 'login',
                requireAuth: false,
                ignoreAddPage: true,
            },
            beforeEnter: (to: any, from: any, next: any) => {
                router.app.$store.commit('resetRootStateData');
                next();
            },
            component: () => import('@components/login/login.vue'),
        },
        {
            path: '/',
            redirect: 'app'
        },
        {
            path: '*',
            redirect: 'app'
        },
    ],
});

// 解决路由跳转路由重复时报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location: any) {
    let result: any = originalPush.call(this, location);
    return result.catch((err: any) => err);
}
export default router;